﻿//文件指定编译编码UTF-8
#pragma execution_character_set("utf-8")

/**
*    @file:    authoritycontroller.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2024-11-08  09:38
*/
#include "authoritycontroller.h"
//#include "authorityservice.h"
#include <QJsonDocument>
#include <authorityservice.h>
#include <responesutils.h>

AuthorityController::AuthorityController(QObject *parent) : HttpRequestHandler(parent)
{

}

AuthorityController::~AuthorityController()
{

}

void AuthorityController::service(HttpRequest &request, HttpResponse &response)
{
    QString path = request.getPath();
    QStringList pathList = path.split("/");
    if (pathList.size() < 3)
    {
        return this->returnError(response);
    }
    QString method = pathList.value(3);
    QByteArray responseMsg;
    QString methodType = request.getMethod();
    if (methodType == "GET")
    {
        qDebug() << method << (method == "pagelist");
        if (method == "pagelist")
        {
            return getPageList(request, response);
        }
        else if (method == "list")
        {
            return getList(request, response);
        }
    }
    else if (methodType == "POST")
    {
        if (method == "save")
        {
            return saveOne(request, response);
        }
    }
    else if (methodType == "DELETE")
    {
        if (method == "delete")
        {
            return deleteOne(request, response);
        }
    }
    this->returnError(response);
}

void AuthorityController::getPageList(HttpRequest &request, HttpResponse &response)
{
    AuthorityService objectService;
    QList<Authority> list;
    if(!objectService.getList(list)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    QJsonArray jsonArray ;
    for(Authority &item:list){
        jsonArray.append(item.toJsonObject());
    }
    return ResponesUtils::pagedResultResponse(response,list.size(),jsonArray);
}

void AuthorityController::getList(HttpRequest &request, HttpResponse &response)
{
    AuthorityService objectService;
    QList<Authority> list;
    if(!objectService.getList(list)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    QJsonArray jsonArray ;
    for(Authority &item:list){
        jsonArray.append(item.toJsonObject());
    }
    return ResponesUtils::resultResponse(response,jsonArray);
}

void AuthorityController::saveOne(HttpRequest &request, HttpResponse &response)
{
    QByteArray body = request.getBody();
    QJsonParseError parseError;
    QJsonDocument  jsonDocument= QJsonDocument::fromJson(body, &parseError);

    if (parseError.error != QJsonParseError::NoError)
    {
        return ResponesUtils::erroResponse(response,"请求参数JSON结构异常!");
    }
    Authority object;
    object.fromJsonObject(jsonDocument.object().value("params").toObject());
    AuthorityService objectService;
    if(!objectService.saveOne(object)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    //返回结果
    ResponesUtils::resultResponse(response,"添加成功！");
}

void AuthorityController::deleteOne(HttpRequest &request, HttpResponse &response)
{
    uint64_t id = request.getParameter("id").toULongLong();
    if(id == 0 ){
        return ResponesUtils::erroResponse(response,"请指定删除ID！");
    }
    Authority object;
    object.setId(id);
    AuthorityService objectService;
    if(!objectService.deleteOne(object)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    //返回结果
    ResponesUtils::resultResponse(response,"删除成功！");
}
